import { SCREEN_WIDTH, SCREEN_HEIGHT } from '../render';

const BTN_WIDTH = 80;
const BTN_HEIGHT = 80;
const MARGIN = 20;

export default class GameControl {
  constructor() {
    this.initButtons();
    this.initTouchEvents();
  }

  initButtons() {
    // 左右移动按钮
    this.leftBtn = {
      x: MARGIN,
      y: SCREEN_HEIGHT - BTN_HEIGHT - MARGIN,
      width: BTN_WIDTH,
      height: BTN_HEIGHT,
      text: '←'
    };

    this.rightBtn = {
      x: MARGIN + BTN_WIDTH + 20,
      y: SCREEN_HEIGHT - BTN_HEIGHT - MARGIN,
      width: BTN_WIDTH,
      height: BTN_HEIGHT,
      text: '→'
    };

    // 跳跃按钮放在右侧
    this.jumpBtn = {
      x: SCREEN_WIDTH - BTN_WIDTH - MARGIN,
      y: SCREEN_HEIGHT - BTN_HEIGHT - MARGIN,
      width: BTN_WIDTH,
      height: BTN_HEIGHT,
      text: '↑'
    };
  }

  initTouchEvents() {
    this.moveDirection = 0;  // -1: 左, 0: 停止, 1: 右
    this.isJumping = false;

    wx.onTouchStart((e) => {
      const touch = e.touches[0];
      const { clientX: x, clientY: y } = touch;
      
      if (this.checkTouchButton(x, y, this.leftBtn)) {
        this.moveDirection = -1;
      } else if (this.checkTouchButton(x, y, this.rightBtn)) {
        this.moveDirection = 1;
      } else if (this.checkTouchButton(x, y, this.jumpBtn)) {
        this.isJumping = true;
      }
    });

    wx.onTouchEnd(() => {
      this.moveDirection = 0;
      this.isJumping = false;
    });
  }

  checkTouchButton(x, y, btn) {
    return x >= btn.x && x <= btn.x + btn.width &&
           y >= btn.y && y <= btn.y + btn.height;
  }

  render(ctx) {
    // 绘制控制按钮
    [this.leftBtn, this.rightBtn, this.jumpBtn].forEach(btn => {
      // 绘制按钮背景
      ctx.beginPath();
      ctx.arc(
        btn.x + btn.width/2,
        btn.y + btn.height/2,
        btn.width/2,
        0,
        2 * Math.PI
      );
      
      // 创建渐变背景
      const gradient = ctx.createRadialGradient(
        btn.x + btn.width/2,
        btn.y + btn.height/2,
        btn.width/4,
        btn.x + btn.width/2,
        btn.y + btn.height/2,
        btn.width/2
      );
      gradient.addColorStop(0, 'rgba(255, 255, 255, 0.9)');
      gradient.addColorStop(1, 'rgba(255, 255, 255, 0.5)');
      
      ctx.fillStyle = gradient;
      ctx.fill();
      
      // 添加边框
      ctx.strokeStyle = 'rgba(255, 255, 255, 0.8)';
      ctx.lineWidth = 2;
      ctx.stroke();

      // 绘制按钮图标
      ctx.save();
      ctx.translate(btn.x + btn.width/2, btn.y + btn.height/2);
      
      // 绘制箭头
      ctx.beginPath();
      ctx.lineWidth = 4;
      ctx.strokeStyle = '#4A90E2';  // 使用蓝色
      
      if (btn.text === '←') {
        // 左箭头
        ctx.moveTo(10, 0);
        ctx.lineTo(-10, 0);
        ctx.moveTo(-10, 0);
        ctx.lineTo(-5, -8);
        ctx.moveTo(-10, 0);
        ctx.lineTo(-5, 8);
      } else if (btn.text === '→') {
        // 右箭头
        ctx.moveTo(-10, 0);
        ctx.lineTo(10, 0);
        ctx.moveTo(10, 0);
        ctx.lineTo(5, -8);
        ctx.moveTo(10, 0);
        ctx.lineTo(5, 8);
      } else if (btn.text === '↑') {
        // 跳跃箭头
        ctx.moveTo(0, 10);
        ctx.lineTo(0, -10);
        ctx.moveTo(0, -10);
        ctx.lineTo(-8, -5);
        ctx.moveTo(0, -10);
        ctx.lineTo(8, -5);
      }
      
      ctx.stroke();
      ctx.restore();
    });
  }
} 